<?php
require '../../config.php';	
$resultset = mysql_query($_REQUEST['query'],$conexion);
$cualtabla = $_REQUEST['cual'];

if(!$resultset)
{
    mysql_close();
    echo "<legend>Sin Resultados</legend>";
}
elseif(!mysql_num_rows($resultset))
{
    mysql_close();
     echo "<legend>Sin Resultados</legend>";
}
else
	
{
	?>
							
							<button type="button" class="btn btn-default" onclick="reportarpdf();" >Imprimir <span class= 'glyphicon glyphicon-print'></span>  </button>
									<?php
	if($cualtabla=="clientes"){
			echo   "<table class='table table-bordered'>";
			
			echo   "<tr>";
			echo   "<th>Cliente</th><th>Fecha Creaci&oacute;n</th><th>Direcci&oacute;n</th><th>Tel&eacute;fono</th><th>Contacto</th><th>Tel. Contacto</th><th>Organizaci&oacute;n</th><th>Tipo</th><th>Ruc</th><th>Dv</th>";
			echo   "</tr>";
			echo   "<tbody>";
			$aux=0;
			while($row = mysql_fetch_assoc($resultset))
			{
				if($aux==0){
					$color="#FFFFFF";
					$aux=1;
				}else{
					$color="#F9F9F9";
					$aux=0;
				}
				echo ("<tr bgcolor='".$color."'>");
				echo   "<td>"._convert($row['nombre'])."</td>";
				echo   "<td>".date("d M Y",strtotime($row['fechacreacion']))."</td>";
				echo   "<td>"._convert($row['direccion'])."</td>";
				echo   "<td>".$row['telefono']."</td>";
				echo   "<td>"._convert($row['contacto'])."</td>";
				echo   "<td>".$row['telcontacto']."</td>";
				if($row['org']==1)$org="Gobierno";
				if($row['org']==2)$org="Privado";
				if($row['org']==3)$org="Natural";
				echo   "<td>".$org."</td>";
				if($row['tipo']==1)$org="Prospecto";
				if($row['tipo']==2)$org="Potencial";
				if($row['tipo']==3)$org="Activo";
				echo   "<td>".$org."</td>";
				echo   "<td>".$row['ruc']."</td>";
				echo   "<td>".$row['dv']."</td>";
				echo   "</tr>";
			}
			echo   "</tbody>";
			echo   "</table>";
			mysql_close();
	}
	elseif ($cualtabla=="contratos")
	{
		echo   "<table class='table table-bordered'>";
		
		echo   "<tr>";
		echo   "<th>Contrato</th><th>Cliente</th><th>Fecha Inicio</th><th>Fecha Fin</th><th>Vendedor</th><th>Monto</th><th>Saldo</th><th>Observaci&oacute;n</th><th>Detalles</th>";
		echo   "</tr>";
		echo   "<tbody>";
		$aux=0;
		while($row = mysql_fetch_assoc($resultset))
		{
			if($aux==0){
				$color="#FFFFFF";
				$aux=1;
			}else{
				$color="#F9F9F9";
				$aux=0;
			}
			echo ("<tr bgcolor='".$color."'>");
			echo   "<td>".$row['idcontratos']."</td>";
			echo   "<td>"._convert($row['nomcliente'])."</td>";
			echo   "<td>".date("d M Y",strtotime($row['fechainicio']))."</td>";
			echo   "<td>".date("d M Y",strtotime($row['fechafin']))."</td>";
			echo   "<td>".$row['nomvendedor']."</td>";
			echo   "<td>$".$row['monto']."</td>";
			echo   "<td>$".$row['saldo']."</td>";
			echo   "<td>"._convert($row['observacion'])."</td>";
			echo   "<td>".$row['medio']." ".$row['tipopropuesta']." ".$row['descripcion']."</td>";
			echo   "</tr>";
		}
		echo   "</tbody>";
		echo   "</table>";
		mysql_close();
	}
	elseif ($cualtabla=="pagos")
	{
		echo   "<table class='table table-bordered'>";
	
		echo   "<tr>";
		echo   "<th>Contrato</th><th>Cliente</th><th>Fecha Pago</th><th>Monto</th><th>Observaci&oacute;n</th><th>Usuario</th>";
		echo   "</tr>";
		echo   "<tbody>";
		$aux=0;
		while($row = mysql_fetch_assoc($resultset))
		{
			if($aux==0){
				$color="#FFFFFF";
				$aux=1;
			}else{
				$color="#F9F9F9";
				$aux=0;
			}
			echo ("<tr bgcolor='".$color."'>");
			echo   "<td>".$row['idcontrato']."</td>";
			echo   "<td>"._convert($row['nombrecliente'])."</td>";
			echo   "<td>".date("d M Y",strtotime($row['fechapago']))."</td>";
			echo   "<td>$".$row['montopago']."</td>";
			echo   "<td>"._convert($row['obspago'])."</td>";
			echo   "<td>".$row['cobrador']."</td>";
			echo   "</tr>";
		}
		echo   "</tbody>";
		echo   "</table>";
		mysql_close();
	}
	elseif ($cualtabla=="clientecontrato")
	{
		echo   "<table class='table table-bordered'>";
	
		echo   "<tr>";
		echo   "<th>Cliente</th><th>Fecha Creaci&oacute;n</th><th>Direcci&oacute;n</th><th>Contrato</th><th>Fecha Inicio</th><th>Fecha Fin</th><th>Monto</th><th>Saldo</th><th>Vendedor</th><th>Observaci&oacute;n</th>";
		echo   "</tr>";
		echo   "<tbody>";
		$aux=0;
		while($row = mysql_fetch_assoc($resultset))
		{
			if($aux==0){
				$color="#FFFFFF";
				$aux=1;
			}else{
				$color="#F9F9F9";
				$aux=0;
			}
			echo ("<tr bgcolor='".$color."'>");
			echo   "<td>"._convert($row['nomcliente'])."</td>";
			echo   "<td>".date("d M Y",strtotime($row['fechacreacion']))."</td>";
			echo   "<td>"._convert($row['direccion'])."</td>";
			echo   "<td>".$row['idcontratos']."</td>";
			echo   "<td>".date("d M Y",strtotime($row['fechainicio']))."</td>";
			echo   "<td>".date("d M Y",strtotime($row['fechafin']))."</td>";
			echo   "<td>$".$row['monto']."</td>";
			echo   "<td>$".$row['saldo']."</td>";
			echo   "<td>".$row['nomvendedor']."</td>";
			echo   "<td>"._convert($row['observacion'])."</td>";
			echo   "</tr>";
		}
		echo   "</tbody>";
		echo   "</table>";
		mysql_close();
	}
		
}
function _convert($content) {
	if(!mb_check_encoding($content, 'UTF-8')
	OR !($content === mb_convert_encoding(mb_convert_encoding($content, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {

		$content = mb_convert_encoding($content, 'UTF-8');

		if (mb_check_encoding($content, 'UTF-8')) {
			// log('Converted to UTF-8');
		} else {
			// log('Could not converted to UTF-8');
		}
	}
	return $content;
}
?>